package com.ece.devirtualizus.Dao;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class SqlLiteUserDao {
	private SQLiteDatabase database;
	private SqlLiteUserHelper dbHelper;

	public SqlLiteUserDao(Context context) {
		dbHelper = new SqlLiteUserHelper(context);
	}

	public void open() throws SQLException {
		boolean isOk = dbHelper.checkDataBase();
		if (isOk) {
			Log.w("SqlLiteUserDao.open", "return true");
			this.database = this.dbHelper.getWritableDatabase();
			//this.dbHelper.destroyDatabase(this.database);
			this.dbHelper.onCreate(this.database);
		} else {
			Log.w("SqlLiteUserDao.open", "return false");
			this.database = this.dbHelper.getReadableDatabase();
		}
	}

	public void createUserField(String login, String motto, String headImId,
			String bodyImId, String legImId) {

		Log.i("SqlLiteUserDao.createUserField","START");
		this.dbHelper.onUpgrade(this.database, 0, 0);
		this.dbHelper.onCreate(this.database);
		
		Log.i("SqlLiteUserDao.createUserField","Values:"+login+" | "+motto);
		ContentValues userValues = new ContentValues();
		userValues.put("login", login);
		userValues.put("motto", motto);
		userValues.put("headImId", headImId);
		userValues.put("bodyImId", bodyImId);
		userValues.put("legImId", legImId);

		long rowUserId;

		rowUserId = this.database.insert("user", null, userValues);

		if (rowUserId == -1) {
			Log.e("createUserField",
					"Erreur lors de le l'inscription du user dans la bdd SqlLite");
		} else {
			Log.i("createUserField",
					"Creation ok, id:" + Long.toString(rowUserId));
		}
	}

	public ArrayList<String> getUserImageId() {
		Log.d("SqlLiteUserDao.getUserImageId", "START");
		ArrayList<String> userData = new ArrayList<String>();
		Cursor cursorUserImageID = this.database.query("user", null, null,
				null, null, null, null);
		cursorUserImageID.moveToFirst();
		while (!cursorUserImageID.isAfterLast()) {
			String headImId = cursorUserImageID.getString(cursorUserImageID
					.getColumnIndex("headImId"));
			String bodyImId = cursorUserImageID.getString(cursorUserImageID
					.getColumnIndex("bodyImId"));
			String legImId = cursorUserImageID.getString(cursorUserImageID
					.getColumnIndex("legImId"));
			userData.add(headImId);
			userData.add(bodyImId);
			userData.add(legImId);
			cursorUserImageID.moveToNext();
		}
		cursorUserImageID.close();
		Log.d("SqlLiteUserDao.getUserImageId", "FINISH");
		return userData;
	}

	public String getUserId() {
		Log.d("SqlLiteUserDao.getUserId", "START");
		String userId = "";

		Cursor cursorUserImageID = this.database.query("user",
				null, null, null, null, null, null);
		cursorUserImageID.moveToFirst();
		while (!cursorUserImageID.isAfterLast()) {
			Log.d("SqlLiteUserDao.getUserId", "in loop");
			userId = cursorUserImageID.getString(cursorUserImageID
					.getColumnIndex("login"));
			Log.d("SqlLiteUserDao.getUserId",  cursorUserImageID.getString(cursorUserImageID
					.getColumnIndex(cursorUserImageID.getColumnName(1))));
			cursorUserImageID.moveToNext();
		}
		Log.d("SqlLiteUserDao.getUserId", userId);
		cursorUserImageID.close();
		Log.d("SqlLiteUserDao.getUserId", "FINISH");
		return userId;
	}
}
